package dnl.dev.memlog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import com.thoughtworks.xstream.XStream;

import dnl.dev.memlog.sampler.MemSampler;
import dnl.dev.memlog.sampler.SamplingInfo;

/**
 * 
 * @author Daniel Orr
 *
 */
public class MemlogProject implements Serializable {

	private transient String projectDirPath;
	private SamplingInfo samplingInfo;
	private SamplingType samplingType;
	private String projectName;
	
	public MemlogProject(String projectName) {
		this.projectName = projectName;
		this.samplingType = SamplingType.PERIODIC;
	}

	public MemlogProject(String projectName, File projDir) {
		this(projectName);
		this.projectDirPath = projDir.getAbsolutePath();
		loadSamplingInfo(projDir);
	}

	private void loadSamplingInfo(File projDir) {
		File[] files = projDir.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".histo");
			}
		});
		for (File f : files) {
			getSamplingInfo().registerDate(f.getName(), new Date(f.lastModified()));
		}
	}

	public String getProjectName() {
		return projectName;
	}

	public void save(){
		if(projectDirPath == null){
			saveImpl(new File("."));
		}
		else {
			saveImpl(new File(projectDirPath));
		}
	}
	
	public void saveImpl(File dir){
		XStream xStream = new XStream();
		String xml = xStream.toXML(this);
		File projDirFile = new File(dir, getProjectName());
		if(!projDirFile.exists()){
			try {
				FileUtils.forceMkdir(projDirFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		this.projectDirPath = projDirFile.getAbsolutePath();
		File projectFile = new File(projectDirPath, "memlog.project");
		try {
			FileUtils.writeStringToFile(projectFile, xml);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static MemlogProject load(File histogramsDir){
		XStream xStream = new XStream();
		File projectFile = new File(histogramsDir, "memlog.project");
		MemlogProject memlogProject = null; 
		if(!projectFile.exists()){
			memlogProject = new MemlogProject("tmp-proj", histogramsDir);
		}
		else {
			try {
				memlogProject =  (MemlogProject) xStream.fromXML(new FileReader(projectFile));
				memlogProject.projectDirPath = histogramsDir.getAbsolutePath();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return memlogProject;
	}
	
	public SamplingType getSamplingType() {
		return samplingType;
	}

	public void setSamplingType(SamplingType samplingType) {
		this.samplingType = samplingType;
	}

	public SamplingInfo getSamplingInfo() {
		if(samplingInfo == null){
			samplingInfo = new SamplingInfo();
		}
		return samplingInfo;
	}

	public String getProjectDirPath() {
		return projectDirPath;
	}

	public File getProjectDir() {
		if(projectDirPath == null){
			throw new IllegalStateException("Project dir was not set.");
		}
		return new File(projectDirPath);
	}

	public Date getFileCreationDate(String filename) {
		return getSamplingInfo().getCreationDate(filename);
	}

	public String getFileCreationLabel(String filename) {
		return filename.substring(0, MemSampler.HISTO_FILE_EXT.length());
	}

}
